package aom.ju.ss.view.base

import android.app.Activity
import android.content.Intent
import android.webkit.*
import aom.ju.ss.R
import aom.ju.ss.databinding.LayoutWebviewBinding

class WebViewActivity : BaseActivity() {

    private lateinit var binding: LayoutWebviewBinding
    override fun init() {
        binding = viewBinding as LayoutWebviewBinding

        val settings = binding.webview.settings
        settings.cacheMode = WebSettings.LOAD_NO_CACHE //设置 缓存模式
        settings.javaScriptEnabled=true
        //DomStorage存储数据要打开
        settings.domStorageEnabled = true
        settings.databaseEnabled = true
        //设置适配
        settings.useWideViewPort = true
        settings.loadWithOverviewMode = true
        binding.webview.webChromeClient = WebChromeClient()
        binding.webview.webViewClient = object : WebViewClient() {
            override fun shouldOverrideUrlLoading(
                view: WebView?,
                request: WebResourceRequest?
            ): Boolean {
                return super.shouldOverrideUrlLoading(view, request)
                view?.loadUrl(intent?.getStringExtra("url")!!)
            }

        }

        binding.webview.loadUrl(intent?.getStringExtra("url")!!)
    }

    override val lay: Int = R.layout.layout_webview

    companion object {
        fun startWeb(activity: Activity, title: String?, webviewUrl: String?) {
            val intent = Intent()
            intent.setClass(activity, WebViewActivity::class.java)
            intent.putExtra("title", title)
            intent.putExtra("url", webviewUrl)
            activity.startActivity(intent)
        }
    }
}